﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    static class ChiTietKhachHangTraLaiBanBuon
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;
        public static Entities.ChiTietKhachHangTraLaiBanBuon[] SelectChiTietKhachHangTraLaiBanBuon(Entities.ChiTietKhachHangTraLaiBanBuon hoadon)
        {
            Entities.ChiTietKhachHangTraLaiBanBuon[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietKhachHangTraLaiBanBuon.sp_Select_ChiTietKhachHangTraLaiBanBuon, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.HanhDong, SqlDbType.NVarChar).Value = hoadon.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.MaKhachHangTraLai, SqlDbType.VarChar).Value = hoadon.MaKhachHangTraLai;
              //  cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.MaHangHoa, SqlDbType.VarChar).Value = hoadon.MaHangHoa;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietKhachHangTraLaiBanBuon row = new Entities.ChiTietKhachHangTraLaiBanBuon();
                    row.ChiTietKhachHangTraLaiBanBuonID = int.Parse(dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.ChiTietKhachHangTraLaiBanBuonID].ToString());
                    row.MaKhachHangTraLai = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.MaKhachHangTraLai].ToString();
                    row.MaHoaDonBanBuon = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.MaHoaDonBanBuon].ToString();
                    row.MaHangHoa = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.TenHangHoa].ToString();
                    row.SoLuong = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.SoLuong].ToString();
                    row.PhanTramChietKhau = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.PhanTramChietKhau].ToString();
                    row.PhanTramChietKhauThuongMai = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.PhanTramChietKhauThuongMai].ToString();
                    row.DonGia = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.DonGia].ToString();
                    row.ThanhTien = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.ThanhTien].ToString();
                    row.Thue = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.Thue].ToString();
                    row.GhiChu = dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.ChiTietKhachHangTraLaiBanBuon.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.ChiTietKhachHangTraLaiBanBuon[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietKhachHangTraLaiBanBuon)arr[i];
                    }
                }
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }

        //public static int InsertUpdateChiTietKhachHangTraLaiBanBuon(Entities.ChiTietKhachHangTraLaiBanBuon hoadon)
        //{
        //    int rt = 0;
        //    try
        //    {
        //        SqlConnection cn = DataAccess.Connection.OpenConnection();
        //        SqlCommand cmd = new SqlCommand("", cn);
        //        rt = cmd.ExecuteNonQuery();
        //        cmd.Connection.Dispose();
        //        cn.Close();
        //        DataAccess.Connection.CloseConnection();
        //    }
        //    catch (Exception ex)
        //    {
        //        string s = ex.Message.ToString();
        //        rt = 0;
        //    }
        //    return rt;
        //}
        public static int InsertUpdateChiTietKhachHangTraLaiBanBuon(Entities.ChiTietKhachHangTraLaiBanBuon[] chititet)
        {
            int kt = 0;
            try
            {
                for (int k = 0; k < chititet.Length; k++)
                {
                    Entities.ChiTietKhachHangTraLaiBanBuon row = new Entities.ChiTietKhachHangTraLaiBanBuon();
                    row.HanhDong = chititet[k].HanhDong;
                    row.MaKho = chititet[k].MaKho;
                    row.MaKhachHangTraLai = chititet[k].MaKhachHangTraLai;
                    row.MaHoaDonBanBuon = chititet[k].MaHoaDonBanBuon;
                    row.MaHangHoa = chititet[k].MaHangHoa;
                    row.TenHangHoa = chititet[k].TenHangHoa;
                    row.SoLuong = chititet[k].SoLuong;
                    row.PhanTramChietKhau = chititet[k].PhanTramChietKhau;
                    row.PhanTramChietKhauThuongMai = chititet[k].PhanTramChietKhauThuongMai;
                    row.DonGia = chititet[k].DonGia;
                    row.ThanhTien = chititet[k].ThanhTien;
                    row.Thue = chititet[k].Thue;
                    row.GhiChu = chititet[k].GhiChu;
                    row.Deleted = chititet[k].Deleted;
                    kt = ThemChiTiet(row);
                    if (kt != 0)
                    { continue; }
                    else
                    { break; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                kt = 0;

            }
            return kt;
        }

        public static int ThemChiTiet(Entities.ChiTietKhachHangTraLaiBanBuon chititethoadonnhap)
        {
            int kt = 0;
            try
            {
                string sql = Common.Constants.ChiTietKhachHangTraLaiBanBuon.sp_ChiTietKhachHangTraLaiBanBuon;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.HanhDong, SqlDbType.VarChar).Value = chititethoadonnhap.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.MaKho, SqlDbType.VarChar).Value = chititethoadonnhap.MaKho;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.MaKhachHangTraLai, SqlDbType.VarChar).Value = chititethoadonnhap.MaKhachHangTraLai;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.MaHoaDonBanBuon, SqlDbType.NVarChar).Value = chititethoadonnhap.MaHoaDonBanBuon;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.MaHangHoa, SqlDbType.NVarChar).Value = chititethoadonnhap.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.TenHangHoa, SqlDbType.VarChar).Value = chititethoadonnhap.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.SoLuong, SqlDbType.Float).Value = chititethoadonnhap.SoLuong;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.PhanTramChietKhau, SqlDbType.Float).Value = chititethoadonnhap.PhanTramChietKhau;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.PhanTramChietKhauThuongMai, SqlDbType.Float).Value = chititethoadonnhap.PhanTramChietKhauThuongMai;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.DonGia, SqlDbType.Float).Value = chititethoadonnhap.DonGia;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.ThanhTien, SqlDbType.Float).Value = chititethoadonnhap.ThanhTien;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.Thue, SqlDbType.Float).Value = chititethoadonnhap.Thue;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.GhiChu, SqlDbType.NVarChar).Value = chititethoadonnhap.GhiChu;
                cmd.Parameters.Add(Common.Constants.ChiTietKhachHangTraLaiBanBuon.Deleted, SqlDbType.Bit).Value = chititethoadonnhap.Deleted;
                kt = cmd.ExecuteNonQuery();
               }
            catch (Exception ex)
            {
                string s = ex.Message;
                kt = 0;

            }
            return kt;
        }

    }
}
#endregion
